package com.itheima.health.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.health.entity.TSetmeal;
import com.itheima.health.service.ITSetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * aliyun 定期删除图片
 */
@Component
public class DeleteTask {
    @Autowired
    ITSetmealService setmealService;
    @Scheduled(cron="0/30 * *  * * ? ")
    public void GetAndDeleteUrl() throws ClientException {
    // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        String accessKeyId="LTAI5tEi6z2GAyPXtK1r6Bod";
        String accessKeySecret="N0ou5HCEKmjajD88UrAYWVPrAOWsmE";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "cpanda";
    // 创建OSSClient实例。
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        // ossClient.listObjects返回ObjectListing实例，包含此次listObject请求的返回结果。
        ObjectListing objectListing = ossClient.listObjects(bucketName);
    try {

        // objectListing.getObjectSummaries获取所有文件的描述信息。
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " +
                    "(size = " + objectSummary.getSize() + ")");
        }
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, "
                + "but was rejected with an error response for some reason.");
        System.out.println("Error Message:" + oe.getErrorMessage());
        System.out.println("Error Code:" + oe.getErrorCode());
        System.out.println("Request ID:" + oe.getRequestId());
        System.out.println("Host ID:" + oe.getHostId());
    }
        List<TSetmeal> setmeals = setmealService.list();
        ArrayList<String> urls = new ArrayList<>();
        Map<String, Integer> map = setmeals.stream().collect(Collectors.toMap(TSetmeal::getImg, TSetmeal::getId));
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            Integer exit = map.get("https://cpanda.oss-cn-beijing.aliyuncs.com/" + objectSummary.getKey());
        if (exit==null){
            urls.add(objectSummary.getKey());
        }
        }
        if (urls==null){
            System.out.println("urls不存在");
            return;
        }
        try {
            System.out.println(urls.toString());
            for (String url : urls) {
                // 删除文件。
                ossClient.deleteObject(bucketName, url);
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}
